package com.runfast.common.utils;

import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;

/**
 * @author: lijin
 * @date: 2018年03月01日
 */
public class DateUtils {

    /**
     * 转换为时间（天,时:分:秒.毫秒）
     *
     * @param timeMillis
     * @return
     */
    public static String formatDateTime(long timeMillis) {
        long day = timeMillis / (24 * 60 * 60 * 1000);
        long hour = (timeMillis / (60 * 60 * 1000) - day * 24);
        long min = ((timeMillis / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (timeMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        long sss = (timeMillis - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000);
        return (day > 0 ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss;
    }


    public static boolean nowTimeBetween(Date start, Date end) {

        LocalTime now = LocalTime.now();

        LocalTime startTime;
        if (start == null) {
            startTime = LocalTime.of(0, 0, 0);
        } else {
            Calendar startCalendar = Calendar.getInstance();
            startCalendar.setTime(start);
            startTime = LocalTime.of(startCalendar.get(Calendar.HOUR_OF_DAY), startCalendar.get(Calendar.MINUTE), startCalendar.get(Calendar.SECOND));
        }


        LocalTime endTime;
        if (end == null) {

            endTime = LocalTime.of(23, 59, 59);
        } else {
            Calendar endCalendar = Calendar.getInstance();
            endCalendar.setTime(end);
            endTime = LocalTime.of(endCalendar.get(Calendar.HOUR_OF_DAY), endCalendar.get(Calendar.MINUTE), endCalendar.get(Calendar.SECOND));
        }

        if (now.isBefore(startTime) && now.isAfter(endTime)) return true;
        return false;
    }
}
